﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="Utils.cs" company="Collaboris Ltd">
// Copyright (c) Collaboris Ltd.  All rights reserved.
// </copyright>
// <summary>Defines the ActionRepository type.</summary>
//-------------------------------------------------------------------------------------------------
namespace Collaboris.SharePoint.Wss
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Xml;

    using Collaboris.Utils;
    using System.Collections.Generic;

    public class Utils
    {
        /// <summary>
        /// Creates NameSpaces necessary to query against
        /// Xml Documents based on the CAML.
        /// </summary>
        /// <returns></returns>
        public static XmlNamespaceManager CreateCAMLNameSpaceMgr(XmlDocument doc)
        {
            ValidationHelper.VerifyObjectArgument(doc, "doc");
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("CAML", "http://schemas.microsoft.com/sharepoint/");
            return nsmgr;
        }

        /// <summary>
        /// Gets the STSAdm console.
        /// </summary>
        /// <returns>int with return value.</returns>
        public static int RunSTSAdmCommand(string[] args)
        {
            string binFolder = Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath("bin");
            string stsAdmPath = Path.Combine(binFolder, "STSAdm.exe");
            List<string> filteredArgs = new List<string>();            
            foreach(string arg in args)
            {
                if (arg.Equals("::IGNORE_KEY::", StringComparison.OrdinalIgnoreCase))
                {
                    // Note: This filteredArgs.Count - 1 cant be negative. 
                    // if it is negative, then something is wrong.
                    filteredArgs.RemoveAt(filteredArgs.Count - 1);
                }
                else if(!arg.Equals("::IGNORE::",StringComparison.OrdinalIgnoreCase)
                    && !arg.Equals("::EMPTY::", StringComparison.OrdinalIgnoreCase)
                    )
                {
                    filteredArgs.Add(arg);                    
                }
            }

            return RunSTSAdmCommand(stsAdmPath, filteredArgs.ToArray());
        }

        /// <summary>
        /// Gets the STSAdm console.
        /// </summary>
        /// <param name="stsAdmPath">The STS adm path.</param>
        /// <param name="args">The arguments to pass into STSADM.</param>
        /// <returns>int with return value.</returns>
        public static int RunSTSAdmCommand(string stsAdmPath, string[] args)
        {
            Assembly stsAdmExe = Assembly.LoadFrom(stsAdmPath);
            Type spAdmin = stsAdmExe.GetType("Microsoft.SharePoint.StsAdmin.SPStsAdmin");
            MethodInfo info = spAdmin.GetMethod("Main");
            if (args != null)
            {
                return (int)info.Invoke(null, new object[] { args });
            }
            return -10;
        }
    }
}